<?php

  class esock{
  	
  	private $host;
  	private $method;
  	private $str;
  	private $page; 
  	private $content;
  	private $cookie;

  	
  	public function init_var($host,$method,$str,$page,$content,$cookie){
  		$this->set_host($host);
  		$this->set_method($method);
  		$this->set_str($str);
  		$this->set_page($page);
  		$this->set_content($content);
  		$this->set_cookie($cookie);
  	}
  	  	
  	private function set_host($hostname){
  		$this->host = $hostname;
  	}
  	
    private function set_method($met){
  		$this->method = $met;
  	}
  	
  	private function set_str($t){
  		$this->str = $t;
  	}
  	
  	private function set_page($page){
  		$this->page = $page;
  	}
  	
  	private function set_content($content){
  		$this->cookie = $content;
  	}
  	
 	private function set_cookie($cookie){
  		$this->cookie = $cookie;
  	}
  	
  	public function esock($host,$method,$str,$page,$content,$cookie){
  		$this->init_var($host,$method,$str,$page,$content,$cookie);
  	}
  	
  	private function GetWebContent($host, $method, $str, $cookie,$hostip=false,$ip_org){
  		
  		if ($hostip)	
  			$ip = $ip_org;
  		else	
  			$ip = gethostbyname($host); 
	    $fp=@fsockopen($ip,80,$errno,$errstr,60); 
	    if (!$fp) return; 
	    
	    
	    fputs($fp, "$method\r\n"); 
	    fputs($fp, "Host: $host\r\n"); 
        fputs($fp, $cookie); 
	    if ( substr(trim($method),0, 4) == "POST") 
	    { 
	        fputs($fp, "Content-Length: ". strlen($str) . "\r\n"); //  别忘了指定长度 
	    }
		fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n"); 
		fputs($fp, "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)\r\n");//add by Ew 071012
		fputs($fp, "Connection: Keep-Alive\r\n\r\n");
	    if ( substr(trim($method),0, 4) == "POST") 
	    { 
	        fputs($fp, $str."\r\n"); 
	    } 
	    while(!feof($fp)) 
	    { 
	        $response .= fgets($fp); 
	    } 
	    $hlen = strpos($response,"\r\n\r\n"); // LINUX下是 "\n\n" 
	    $header = substr($response, 0, $hlen); 
	    $entity = substr($response, $hlen + 4); 
	    
	    //echo $header;
	    //echo "\r\n\r\n\r\n\r\n";
	    
	    
	    $a['content'] = $entity;  
	    $a['header'] = $header;
	    fclose($fp); 
	    return $a; 
  	}
  	
  	public function get_con($met,$str,$hostip=false,$ip=''){
  		$sd = $this->GetWebContent($this->host,$met,$str,$this->cookie,$hostip,$ip);
  		$this->content = $sd['content'];
  		$this->set_str($str);
  		return $this->content;
  	}
  	
  }
  
  
?>


